﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        //画坐标：originX,originY表示坐标原点在界面上的位置，lengthX，lengthY表示横纵坐标的长度，stepX，stepY表示横纵方向上的步长,stepXS,stepYS表示每个步长所代表的实际值
        public void DrawCoordinate(Graphics g, Pen p, int originX, int originY, int lengthX, int lengthY, int stepX, int stepY, double stepXS, double stepYS)
        {
            Font font1 = new Font("宋体",5);
            p.Width = 2;
            int endY;//纵坐标上最上面一点的纵向位置
            int endX;//横坐标上最后一点的横向位置
            if (originY - lengthY < 0)
            {
                endY = 0;
            }
            else endY = originY - lengthY;
            endX = originX + lengthX;
            //画纵坐标线

            g.DrawLine(p, new Point(originX, originY),new Point(originX,endY));
            //画纵坐标刻度步长
            int tempY;
            tempY=originY;
            int i=0;//统计步长数
            while (tempY > 0)
            {
                tempY = originY-(stepY * i);
                if(i==0)
                {
                     g.DrawString("0",font1,Brushes.Black,new Point(originX-5,originY+5));//标刻度0
                }
                else
                {
                    g.DrawLine(p,new Point(originX-5,tempY),new Point(originX,tempY));//画刻度
                    p.Width = 1;
                    g.DrawLine(p, new Point(originX, tempY), new Point(endX, tempY));//画等值线
                    p.Width=2;
                    g.DrawString((stepYS*i).ToString(),font1,Brushes.Black,new Point(originX-15,tempY));//标刻度
                }
                i++;
            }
            //画横坐标线

            g.DrawLine(p, new Point(originX, originY), new Point(endX, originY));
            //画横坐标刻度步长
            int tempX;
            tempX = originX;
            int j = 0;
            while (tempX < endX)
            {
                tempX = originX + (stepX * j);
                if (j == 0) { }
                else
                {
                    g.DrawLine(p, new Point(tempX, originY + 5), new Point(tempX, originY));//画刻度
                    g.DrawString((stepXS*j).ToString(),font1,Brushes.Black,new Point(tempX,originY+10));//标刻度
                }
                j++;
            }
        }
        //画曲线：String name表示曲线的名字，Point pName表示曲线名字的位置，int originX, int originY表示原点坐标， double perPixelY表示纵每个像素所代表的实际数值,stepX表示横纵方向上的步长。double[] data表示曲线数据
        public void DrawCurve(Graphics g, Pen p,String name,Point pName, int originX, int originY, double perPicelY,int stepX,double[] data)
        {
            Font font1 = new Font("宋体",10);
            //标识线条的属性
            g.DrawString(name,font1,Brushes.Black,pName);
            Pen p1 = new Pen(p.Color,3);
            g.DrawLine(p1,pName.X-10,pName.Y,pName.X,pName.Y);
            //画曲线
            int tempX1;
            int tempY1;
            int tempX2;
            int tempY2;
            for (int i = 0; i < data.Length-1; i++)
            {
                tempX1 = originX + i * stepX;
                tempY1 = (int)(originY - (data[i] / perPicelY));//计算起点的纵坐标
               
                tempX2 = tempX1 + stepX;
                tempY2=(int)(originY-(data[i+1]/perPicelY));//计算终点的坐标
                 //绘制点所在的小圆圈
                Rectangle r = new Rectangle(tempX2-3,tempY2-3,6,6);
                g.DrawEllipse(p, r);
                //画两点间的线
                g.DrawLine(p,new Point(tempX1,tempY1),new Point(tempX2,tempY2));

            }
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Blue,2);
            DrawCoordinate(g,p,50,200,400,200,20,20,2.5,5.0);
            double perPicelY = 5.0 / 20;
            double[] data={10,4,6,23,3,14,4,6};
            DrawCurve(g, p, "曲线1", new Point(10, 10), 50, 200, perPicelY, 20, data);
            double[] data1 = { 23,45,6,7,33,6,27,2,15,23,4,6,24}; 
            DrawCurve(g,Pens.Black,"曲线2",new Point(10,50),50,200,perPicelY,20,data1);

        }
    }
}
